package edu.gatech.cs2340.group7.wheresmystuffapp.presenters;

import edu.gatech.cs2340.group7.wheresmystuffapp.model.Admin;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DataManager;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DatabaseHelper;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.CreateAdminView;

/**
 * Presenter for creating an admin
 * 
 * @author tbowling3
 * 
 */
public class CreateAdminPresenter {
	private CreateAdminView myView;
	private DatabaseHelper helper;

	public CreateAdminPresenter(CreateAdminView viewIn) {
		myView = viewIn;
		helper = new DatabaseHelper(myView);
	}

	/**
	 * Handle the Create Admin button from the UI view
	 * 
	 * @param email
	 *            Username for the admin to be created
	 * @param password
	 *            Password for the admin to be created
	 * @param passwordConfirm
	 *            Confirmation of the password for the admin to be created
	 * @param phone
	 *            Phone number for the admin to be created
	 */
	public void createAdmin(String email, String password,
			String passwordConfirm, String phone) {
		boolean correctData = checkInputs(email, password, passwordConfirm,
				phone);

		if (correctData) { // Create admin
			Admin newAdmin = new Admin(email, password, phone);
			helper.addAccount(newAdmin);
		}
	}

	/**
	 * Checks to see if the user has input valid information. Also checks to see
	 * if the username already exists in the system
	 * 
	 * @param username
	 *            The username trying to be created
	 * @param password
	 *            The proposed password
	 * @param confPassword
	 *            The confirmation of the proposed password
	 * @param phoneNumber
	 *            The user's phone number
	 * @return True if all the information is valid and false otherwise
	 */
	private boolean checkInputs(String username, String password,
			String confPassword, String phoneNumber) {
		boolean out = true;

		if (username != null && password != null && confPassword != null
				&& phoneNumber != null) { // If all the inputs are non-null

			if (helper.accountExists(username)) { // If the username
														// already exists in the
														// database
				myView.setResultText("The account already exists");
				out = false;
			} else if (!password.equals(confPassword)) { // if the passwords
															// don't match
				myView.setResultText("The passwords do not match");
				out = false;
			} else if (phoneNumber.length() < 7) { // TODO If the phone number
													// is incorrectly given
				out = false;
			}
		} else {
			myView.setResultText("Invalid Information!");
			out = false;
		}

		return out;
	}
}
